home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / ImageAttachment.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  3.2 KB  |  129 lines  |  [TEXT/CWIE]

  1. // ImageAttachment.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9.  
  10. /** TextAttachment subclass for placing an Image among a TextView's characters.
  11.   */
  12. public class ImageAttachment extends TextAttachment implements Codable {
  13.     private Image image;
  14.     boolean incrementalBitmap;
  15.     int     width,height;
  16.  
  17.     final static String IMAGE_KEY = "image";
  18.  
  19.     /** Constructs an empty ImageAttachment.
  20.       */
  21.     public ImageAttachment() {
  22.         super();
  23.         image = null;
  24.         incrementalBitmap = false;
  25.     }
  26.  
  27.     /** Constructs an ImageAttachment containing <b>anImage</b>.
  28.       */
  29.     public ImageAttachment(Image anImage) {
  30.         super();
  31.         image = anImage;
  32.         incrementalBitmap = false;
  33.     }
  34.  
  35.     /** Constructs an ImageAttachment that loads <b>aBitmap</b>
  36.       * incrementally.
  37.       */
  38.     ImageAttachment(Bitmap aBitmap,int width,int height) {
  39.         super();
  40.         image = aBitmap;
  41.         this.width = width;
  42.         this.height= height;
  43.         incrementalBitmap = true;
  44.     }
  45.  
  46.     /** Set the ImageAttachment's Image to <b>anImage</b>.
  47.       */
  48.     public void setImage(Image anImage) {
  49.         image = anImage;
  50.     }
  51.  
  52.     /** Returns the ImageAttachment's Image.
  53.       * @see #setImage
  54.       */
  55.     public Image image() {
  56.         return image;
  57.     }
  58.  
  59.     /** Overridden to return the ImageAttachment's width.
  60.       */
  61.     public int width() {
  62.         if( incrementalBitmap )
  63.             return width;
  64.  
  65.         if( image != null )
  66.             return image.width();
  67.         else
  68.             return 0;
  69.     }
  70.  
  71.     /** Overridden to return the ImageAttachment's height.
  72.       */
  73.     public int height() {
  74.         if( incrementalBitmap )
  75.             return height;
  76.  
  77.         if( image != null )
  78.             return image.height();
  79.         else
  80.             return 0;
  81.     }
  82.  
  83.     /** Draws the ImageAttachment's Image.
  84.       */
  85.     public void drawInRect(Graphics g, Rect boundsRect) {
  86.         Rect clipRect;
  87.         if (g == null || boundsRect == null) {
  88.             return;
  89.         }
  90.         clipRect = g.clipRect();
  91.         if( image != null ) {
  92.             image.drawAt(g,boundsRect.x,boundsRect.y);
  93.         }
  94.     }
  95.  
  96.     /** Describes the ImageAttachment class' information.
  97.       * @see Codable#describeClassInfo
  98.       */
  99.     public void describeClassInfo(ClassInfo info) {
  100.         super.describeClassInfo(info);
  101.         info.addClass("netscape.application.ImageAttachment", 1);
  102.         info.addField(IMAGE_KEY,OBJECT_TYPE);
  103.     }
  104.  
  105.     /** Encodes the ImageAttachment instance.
  106.       * @see Codable#encode
  107.       */
  108.     public void encode(Encoder encoder) throws CodingException {
  109.         super.encode(encoder);
  110.         encoder.encodeObject(IMAGE_KEY,(Codable)image);
  111.     }
  112.  
  113.     /** Decodes the ImageAttachment instance.
  114.       * @see Codable#decode
  115.       */
  116.     public void decode(Decoder decoder) throws CodingException {
  117.         super.decode(decoder);
  118.         image = (Image) decoder.decodeObject(IMAGE_KEY);
  119.     }
  120.  
  121.     /** Finishes the ImageAttachment's decoding.
  122.       * @see Codable#finishDecoding
  123.       */
  124.     public void finishDecoding() throws CodingException {
  125.         super.finishDecoding();
  126.         incrementalBitmap = false;
  127.     }
  128. }
  129.